package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.models.BottleSetting;
import com.moleq.models.Departmant;
import com.moleq.models.ItemTareSetting;
import com.moleq.models.TaxSetting;

public class ItemTareService {
	private DatabaseHelper DBHelper;

	public ItemTareService() {

	}

	public ItemTareService(Context context) {
		this.DBHelper = new DatabaseHelper(context);
	}
	
	public List<ItemTareSetting> FindAll(){
		List<ItemTareSetting> obj = new ArrayList<ItemTareSetting>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from t_item_tare_setting",null);
		
		while(cursor.moveToNext()){
			int it_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemTareSetting.IT_NO));
			String upc  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemTareSetting.UPC));
			int it_tare_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemTareSetting.IT_TARE_NO));
			String create_datetime = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemTareSetting.CREATE_TIME));
			String update_datetime = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemTareSetting.UPDATE_TIME));
			
			obj.add(new ItemTareSetting(it_no,upc,it_tare_no,create_datetime,update_datetime));
		}
		cursor.close();
		return obj;
	}
	
	public List<ItemTareSetting> FindTateByUPC(String supc){
		List<ItemTareSetting> obj = new ArrayList<ItemTareSetting>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from t_item_tare_setting where F01 = '" + supc + "'" ,null);
		
		while(cursor.moveToNext()){
			int it_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemTareSetting.IT_NO));
			String upc  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemTareSetting.UPC));
			int it_tare_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemTareSetting.IT_TARE_NO));
			String create_datetime = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemTareSetting.CREATE_TIME));
			String update_datetime = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemTareSetting.UPDATE_TIME));
			
			obj.add(new ItemTareSetting(it_no,upc,it_tare_no,create_datetime,update_datetime));
		}
		cursor.close();
		return obj;
	}
	
	public ItemTareSetting Find(int no){
		
		ItemTareSetting obj = new ItemTareSetting();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from t_item_tare_setting where it_no  = "+no,null);
		while(cursor.moveToNext()){
			int it_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemTareSetting.IT_NO));
			String upc  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemTareSetting.UPC));
			int it_tare_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemTareSetting.IT_TARE_NO));
			String create_datetime = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemTareSetting.CREATE_TIME));
			String update_datetime = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemTareSetting.UPDATE_TIME));
			
			obj = (new ItemTareSetting(it_no,upc,it_tare_no,create_datetime,update_datetime));
					
		}
		cursor.close();
		return obj;
	
	}
	
	public String GetSingleTare(String upc){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select b.ta_desc from t_item_tare_setting as a,t_tare_setting as b"
				+ " where a.it_tare_no = b.ta_no and F01 = '"
				+ upc + "'", null);
		if (cursor.moveToNext()) {
			String result = cursor.getString(0);
			cursor.close();
			return result;
		}
		cursor.close();
		return "";
	}
	
	public List<ItemTareSetting> GetMultipleTare(String upc) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select a.it_no,b.ta_desc,b.ta_no,a.F01,b.ta_value from t_item_tare_setting as a,t_tare_setting as b"
				+ " where a.it_tare_no = b.ta_no and F01 = '"
				+ upc + "'", null);
		Cursor cursor2 = db.rawQuery("select ps_tare_rate from t_pos_setting", null);
		cursor2.moveToFirst();
		List<ItemTareSetting> itemTareList = new ArrayList<ItemTareSetting>();
		while (cursor.moveToNext()) {
			ItemTareSetting item = new ItemTareSetting();
			item.setIt_no(cursor.getInt(0));
			item.setDesc(cursor.getString(1));
			item.setIt_tare_no(cursor.getInt(2));
			item.setUpc(cursor.getString(3));
			item.setValue(String.format("%.2f", cursor.getInt(4)
					* cursor2.getFloat(0)));
			itemTareList.add(item);
		}
		cursor.close();
		cursor2.close();
		return itemTareList;
	}
}
